Importing Data and Libraries

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
mnprescounty2012 <- read.csv("~/Downloads/mnprescounty2012.csv", header=FALSE)
mnprescounty2016 <- read.csv("~/Downloads/mnprescounty2016.csv", header=FALSE)
povertyincome2012 <- read.csv("~/Downloads/2012_povertyincome.csv", header=FALSE)
povertyincome2016 <- read.csv("~/Downloads/povertyincome_2016.csv", header=FALSE)

Cleaning up dataframes

#Renaming Columns 
Pres_2012 <- rename(mnprescounty2012, State = V1, County = V2, Office_ID = V4, Office_Name = V5, Cand_ID = V7, Cand_Name = V8, Party_ID = V11, Precincts_Reporting = V12, Total_Precincts = V13, Votes_For_Cand = V14, Perc_Votes_Cand_By_Office = V15, Total_Votes_For_Office = V16)

Pres_2016 <- rename(mnprescounty2016, State = V1, County = V2, Office_ID = V4, Office_Name = V5, Cand_ID = V7, Cand_Name = V8, Party_ID = V11, Precincts_Reporting = V12, Total_Precincts = V13, Votes_For_Cand = V14, Perc_Votes_Cand_By_Office = V15, Total_Votes_For_Office = V16)
#Removing NA Columns or Columns we don't need 
Pres_Data_2012 <- select(Pres_2012,County, Office_ID,Cand_ID, Cand_Name, Precincts_Reporting, Total_Precincts, Votes_For_Cand, Total_Votes_For_Office)

Pres_Data_2016 <- select(Pres_2016,County, Office_ID,Cand_ID, Cand_Name, Precincts_Reporting, Total_Precincts, Votes_For_Cand, Total_Votes_For_Office)

#Converting County ID to factor so we can rename the levels for actual County name instead of number
Pres_Data_2012$County <- as.factor(Pres_Data_2012$County)
Pres_Data_2016$County <- as.factor(Pres_Data_2016$County)
#More cleaning up of other dataframes
povinc_2012 <- rename(povertyincome2012, Poverty_All = V3, Med_Household_Inc = V21,County = V24)
filter_2012 <- povinc_2012 %>%
  select(Poverty_All,Med_Household_Inc,County)
#removing first row as it contained irrelevant information
pov_inc_2012 <- filter_2012[-c(1), ]

data_2012 <- pov_inc_2012 %>%
  mutate(County = str_remove_all(County, "County")) %>%
  mutate(County = str_to_lower(County)) %>%
  mutate(County = str_trim(County, side = c("both")))

#Doing the same cleaning for 2016 data
povinc_2016 <- rename(povertyincome2016,Poverty_All = V3,Med_Household_Inc = V21,County = V24)
filter_2016 <- povinc_2016 %>%
  select(Poverty_All, Med_Household_Inc, County)
pov_inc_2016 <- filter_2016[-c(1), ]
data_2016 <- pov_inc_2016 %>%
  mutate(County = str_remove_all(County, "County")) %>%
  mutate(County = str_to_lower(County)) %>%
  mutate(County = str_trim(County, side = c("both")))
#renaming factor levels
presdata2012 <- Pres_Data_2012 %>% 
  mutate(County = fct_recode(County,
                                "Aitkin" = "1",
                                "Anoka" = "2",
                                "Becker" = "3",
                                "Beltrami" = "4",
                                "Benton" = "5",
                                "Big Stone" = "6",
                                "Blue Earth" = "7",
                                "Brown" = "8",
                                "Carlton" = "9",
                                "Carver" = "10",
                                "Cass" = "11",
                                "Chippewa" = "12",
                                "Chisago" = "13",
                                "Clay" = "14",
                                "Clearwater" = "15",
                                "Cook" = "16",
                                "Cottonwood" = "17",
                                "Crow Wing" = "18",
                                "Dakota" = "19",
                                "Dodge" = "20",
                                "Douglas" = "21",
                                "Faribault" = "22",
                                "Fillmore" = "23",
                                "Freeborn" = "24",
                                "Goodhue" = "25",
                                "Grant" = "26",
                                "Hennepin" = "27",
                                "Houston" = "28",
                                "Hubbard" = "29",
                                "Isanti" = "30",
                                "Itasca" = "31",
                                "Jackson" = "32",
                                "Kanabec" = "33",
                                "Kandiyohi" = "34",
                                "Kittson" = "35",
                                "Koochiching" = "36",
                                "Lac qui Parle" = "37",
                                "Lake" = "38",
                                "Lake of the Woods" = "39",
                                "Le Sueur" = "40",
                                "Lincoln" = "41",
                                "Lyon" = "42",
                                "McLeod" = "43",
                                "Mahnomen" = "44",
                                "Marshall" = "45",
                                "Martin" = "46",
                                "Meeker" = "47",
                                "Mille Lacs" = "48",
                                "Morrison" = "49",
                                "Mower" = "50",
                                "Murray" = "51",
                                "Nicollet" = "52",
                                "Nobles" = "53",
                                "Norman" = "54",
                                "Olmsted" = "55",
                                "Otter Tail" = "56",
                                "Pennington" = "57",
                                "Pine" = "58",
                                "Pipestone" = "59",
                                "Polk" = "60",
                                "Pope" = "61",
                                "Ramsey" = "62",
                                "Red Lake" = "63",
                                "Redwood" = "64",
                                "Renville" = "65",
                                "Rice" = "66",
                                "Rock" = "67",
                                "Roseau" = "68",
                                "Saint Louis" = "69",
                                "Scott" = "70",
                                "Sherburne" = "71",
                                "Sibley" = "72",
                                "Stearns" = "73",
                                "Steele" = "74",
                                "Stevens" = "75",
                                "Swift" = "76",
                                "Todd" = "77",
                                "Traverse" = "78",
                                "Wabasha" = "79",
                                "Wadena" = "80",
                                "Waseca" = "81",
                                "Washington" = "82",
                                "Watonwan" = "83",
                                "Wilkin" = "84",
                                "Winona" = "85",
                                "Wright" = "86",
                                "Yellow Medicine" = "87"
                                
                                )) %>%
   mutate(County = str_to_lower(County)) %>%
   mutate(County = str_remove_all(County, "//s"))

presdata2016 <- Pres_Data_2016 %>% 
  mutate(County = fct_recode(County,
                                "Aitkin" = "1",
                                "Anoka" = "2",
                                "Becker" = "3",
                                "Beltrami" = "4",
                                "Benton" = "5",
                                "Big Stone" = "6",
                                "Blue Earth" = "7",
                                "Brown" = "8",
                                "Carlton" = "9",
                                "Carver" = "10",
                                "Cass" = "11",
                                "Chippewa" = "12",
                                "Chisago" = "13",
                                "Clay" = "14",
                                "Clearwater" = "15",
                                "Cook" = "16",
                                "Cottonwood" = "17",
                                "Crow Wing" = "18",
                                "Dakota" = "19",
                                "Dodge" = "20",
                                "Douglas" = "21",
                                "Faribault" = "22",
                                "Fillmore" = "23",
                                "Freeborn" = "24",
                                "Goodhue" = "25",
                                "Grant" = "26",
                                "Hennepin" = "27",
                                "Houston" = "28",
                                "Hubbard" = "29",
                                "Isanti" = "30",
                                "Itasca" = "31",
                                "Jackson" = "32",
                                "Kanabec" = "33",
                                "Kandiyohi" = "34",
                                "Kittson" = "35",
                                "Koochiching" = "36",
                                "Lac qui Parle" = "37",
                                "Lake" = "38",
                                "Lake of the Woods" = "39",
                                "Le Sueur" = "40",
                                "Lincoln" = "41",
                                "Lyon" = "42",
                                "McLeod" = "43",
                                "Mahnomen" = "44",
                                "Marshall" = "45",
                                "Martin" = "46",
                                "Meeker" = "47",
                                "Mille Lacs" = "48",
                                "Morrison" = "49",
                                "Mower" = "50",
                                "Murray" = "51",
                                "Nicollet" = "52",
                                "Nobles" = "53",
                                "Norman" = "54",
                                "Olmsted" = "55",
                                "Otter Tail" = "56",
                                "Pennington" = "57",
                                "Pine" = "58",
                                "Pipestone" = "59",
                                "Polk" = "60",
                                "Pope" = "61",
                                "Ramsey" = "62",
                                "Red Lake" = "63",
                                "Redwood" = "64",
                                "Renville" = "65",
                                "Rice" = "66",
                                "Rock" = "67",
                                "Roseau" = "68",
                                "Saint Louis" = "69",
                                "Scott" = "70",
                                "Sherburne" = "71",
                                "Sibley" = "72",
                                "Stearns" = "73",
                                "Steele" = "74",
                                "Stevens" = "75",
                                "Swift" = "76",
                                "Todd" = "77",
                                "Traverse" = "78",
                                "Wabasha" = "79",
                                "Wadena" = "80",
                                "Waseca" = "81",
                                "Washington" = "82",
                                "Watonwan" = "83",
                                "Wilkin" = "84",
                                "Winona" = "85",
                                "Wright" = "86",
                                "Yellow Medicine" = "87"
                                
                                )) %>%
   mutate(County = str_to_lower(County))
#Making Interactive Map

#Could choose the county and find information based on what county you chose.

#Also a graph that shows some type of change over time and we make a inference from that?? If any states swung a certain way/any changes in poverty???
#Joining Data
all_data_2012 <- full_join(presdata2012, data_2012)
## Joining with `by = join_by(County)`
all_data_2016 <- full_join(presdata2016, data_2016)
## Joining with `by = join_by(County)`
#Filtering candidates down to only republican/democrat main. tickets

new_2012 <- all_data_2012 %>%
  filter(Cand_ID %in% c(401,301))

new_2016 <- all_data_2016 %>%
  filter(Cand_ID %in% c(401,301))
#Filtering data for there to be 1 row for each county; keeping the row with whichever candidate won that county
final_2012 <- new_2012 %>%
  group_by(County) %>%
  slice_max(order_by = Votes_For_Cand, n = 1, with_ties = FALSE) %>%
  ungroup()
#Doing same thing for 2016 dataset

final_2016 <- new_2016 %>%
  group_by(County) %>%
  slice_max(order_by = Votes_For_Cand, n = 1, with_ties = FALSE) %>%
  ungroup()
library(shiny)
library(leaflet)
library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(ggplot2)
library(dplyr)
library(plotly)
library(patchwork)
county_df <- map_data("county")
counties <- county_df %>%
  filter(region == "minnesota") 
  
  
counties_new <- rename(counties, County = subregion)

counties_final <- counties_new %>%
  select(long, lat, County, group) %>%
  mutate(County = fct_recode(County,
                             "saint louis" = "st louis"
                             ))
#Joining long/lat data with previous datasets for each year to create interactive maps.

joined_2012 <- full_join(final_2012, counties_final)
## Joining with `by = join_by(County)`
joined_2012$Cand_Name <- as.factor(joined_2012$Cand_Name)

#Same for 2016 data

joined_2016 <- full_join(final_2016, counties_final)
## Joining with `by = join_by(County)`
#Making both plots for each year
mn_2012 <- ggplot(joined_2012, aes(x = long, y = lat, group = group )) +
  geom_polygon(color = "black",aes(fill = Cand_Name, text = paste(
          "County:",County, "<br>",
          "Number of Individuals in Poverty:",Poverty_All, "<br>",
          "Median Income:$",Med_Household_Inc, "<br>",
          "Candidate: ", Cand_Name))) +
  scale_fill_manual(values = c("#0015bc", "#ff0000")) +
  theme(axis.title.x.bottom = element_blank(), axis.title.y.left = element_blank()) +
  labs(title = "2012 Election Minnesota", legend = "Candidate Name", fill = "Candidate Names") +
  theme_void()
## Warning in geom_polygon(color = "black", aes(fill = Cand_Name, text =
## paste("County:", : Ignoring unknown aesthetics: text
mn_2016 <- ggplot(joined_2016, aes(x = long, y = lat, group = group )) +
  geom_polygon(color = "black",aes(fill = Cand_Name)) +
  scale_fill_manual(values = c("#ff0000", "#0015bc")) +
  theme(axis.title.x.bottom = element_blank(), axis.title.y.left = element_blank()) +
  labs(title = "2016 Election Minnesota", legend = "Candidate Name", fill = "Candidate Names") +
  theme_void()


ggplotly(mn_2012)